<template>
	<view style="background: #fff; box-sizing: border-box; padding: 10px;">
		<!-- <input type="text" placeholder=""> -->
		<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
		<u-form
				labelPosition="left"
				:model="model1"
				labelWidth='180rpx'
				ref="form1"
		>
			<u-form-item
					label="收货人："
					prop="userInfo.linkPeope"
					borderBottom
					ref="item1"
			>
				<u-input
						v-model="model1.userInfo.linkPeope"
						placeholder="请输入收货人姓名"
						:border="false"
				></u-input>
			</u-form-item>
			<u-form-item
					label="联系电话："
					prop="userInfo.phoneNumber"
					borderBottom
					ref="item1"
			>
				<u-input
						type="number"
						maxlength="11"
						v-model="model1.userInfo.phoneNumber"
						placeholder="请输入手机号"
						:border="false"
				></u-input>
			</u-form-item>
			<u-form-item
			       @click="change"
					label="所在位置："
					prop="userInfo.sex"
					borderBottom
					ref="item1"
			>
				<pickerAddress @change="change">{{txt}}</pickerAddress>
				<u-icon
						slot="right"
						name="arrow-right"
				></u-icon>
			</u-form-item>
			
			<u-form-item
					label="详细地址："
					prop="userInfo.address"
					borderBottom
					ref="item1"
			>
				<u-input
						v-model="model1.userInfo.address"
						placeholder="请输入详细地址"
						:border="false"
				></u-input>
			</u-form-item>
			
			<u-form-item
					label="设为默认地址"
					borderBottom
					ref="item1"
			>
			<view class="fr">
				<u-switch active-color="#FA9700" style="float: right;" v-model="model1.userInfo.isDefault" @change="change"></u-switch>
			</view>
				
			</u-form-item>	
			<u-form-item style="margin-top: 60px;">
				<button @click="submit" style="background: #FA9700; color: #fff; width: 90%; border-radius: 30px;line-height: 46px;" type="default">保存</button>	
			</u-form-item>		
				
		</u-form>
	</view>
</template> 

<script>
// import pickerAddress from 'pickerAddress.vue'
import pickerAddress from '@/components/pickerAddress.vue'
import {addDdress,addInfo,changeAdree} from "@/common/config/request.js"
// import {address,addressInfo,addressPut} from '@/components/request.js';
export default {
	components:{
	    pickerAddress
    },
	data() {
		return {
			txt: '选择地址',
			title: 'Hello',
			showSex: false,
			model1: {
				userInfo: {
					linkPeope: '',
					phoneNumber: '',
					address:'',
					provinceName:'',
					cityName:'',
					countyName:'',
					isDefault:true,
				},
			},
			id:'',
			actions: [],
			rules: {
				
			},
			radio: '',
			switchVal: false
		};
	},
	onLoad(e) {
		console.log(e)
		this.id=e.id
		if(e.id){
			this.addressInfo({id:e.id})
		}
	},
	methods: {
		addressInfo(id){
			addInfo(id).then((e)=>{
				console.log(e)
				if(e.data.code=1){
					if(e.data.data.is_default=='1'){
						e.data.data.isDefault=true
					}else{
						e.data.data.isDefault=false
					}
					let adds=e.data.data.address.split(',')
					this.txt=adds[0]+adds[1]+adds[2]
					this.model1.userInfo.address=adds[3]
					this.model1.userInfo.linkPeope=e.data.data.name
					this.model1.userInfo.phoneNumber=e.data.data.phone
					this.model1.userInfo.provinceName=adds[0]
					this.model1.userInfo.cityName=adds[1]
					this.model1.userInfo.countyName=adds[2]
					// this.model1.userInfo=e.data.data
				}
			})
		},
		
		sexSelect(e) {
			this.model1.userInfo.sex = e.name
			this.$refs.form1.validateField('userInfo.sex')
		},
		change(data) {
			console.log(data)
			this.txt = data.data.toString()
			this.model1.userInfo.provinceName=data.data[0]
			this.model1.userInfo.cityName=data.data[1]
			this.model1.userInfo.countyName=data.data[2]
			console.log(data.data.join(''))
		},
		// 添加收货地址
		address(data){
			addDdress(data).then((e)=>{
				if(e.data.code==1){
					// console.log('22222222222222222222222')
					uni.showToast({
						icon:"none",
						title:'添加成功'
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1500)
				}
			})
		},
		addressPut(data){
			changeAdree(data).then((e)=>{
				if(e.data.code==1){
					// console.log('2222222222222222')
					uni.showToast({
						icon:"none",
						title:'修改成功'
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1500)
				}
			})
		},
		submit(){
			if(this.model1.userInfo.linkPeope==''){
				return uni.showToast({
							  title:'请输入姓名',
							  icon: 'none',
							  duration:1000,
							  mask: true
							})
			}
			var pattern = /^1[345678]\d{9}$/;
			
			if(!pattern.test(this.model1.userInfo.phoneNumber)){ 
				return uni.showToast({
							  title:'手机号错误',
							  icon: 'none',
							  duration:1000,
							  mask: true
							}) 
			}
			
			if(this.model1.userInfo.linkPeope==''){
				return uni.showToast({
							  title:'请输入姓名',
							  icon: 'none',
							  duration:1000,
							  mask: true
							})
			}
			if(this.txt=='选择地址'){
				return uni.showToast({
							  title:'请选择地址',
							  icon: 'none',
							  duration:1000,
							  mask: true
							})
			}
			if(this.model1.userInfo.address==''){
				return uni.showToast({
							  title:'请输入详细地址',
							  icon: 'none',
							  duration:1000,
							  mask: true
							})
			}
			if(this.model1.userInfo.isDefault==false){
				this.model1.userInfo.isDefault=0
			}else{
				this.model1.userInfo.isDefault=1
			}
			if(this.id){
				let datas={
					id:this.id,
					name:this.model1.userInfo.linkPeope,
					phone:this.model1.userInfo.phoneNumber,
					address:this.model1.userInfo.provinceName+','+this.model1.userInfo.cityName+','+this.model1.userInfo.countyName+','+this.model1.userInfo.address,
					is_default:this.model1.userInfo.isDefault,
				}
				this.addressPut(datas)
			}else{
				// console.log(this.model1.userInfo)
				let data={
					name:this.model1.userInfo.linkPeope,
					phone:this.model1.userInfo.phoneNumber,
					address:this.model1.userInfo.provinceName+','+this.model1.userInfo.cityName+','+this.model1.userInfo.countyName+','+this.model1.userInfo.address,
					is_default:this.model1.userInfo.isDefault,
				}
				this.address(data)
			}
			
		},
	},
};
</script>